Conditions | 3 |
Total Lines | 105 |
Code Lines | 91 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import React from 'react'; |
||
6 | |||
7 | export default function Wallet({navigation}): any { |
||
8 | const [balance, setBalance] = useState(null); |
||
9 | const [modalVisible, setModalVisible] = useState(false); |
||
10 | const [prepaid, setPrepaid] = useState(null); |
||
11 | |||
12 | async function getBalance(): Promise<void> { |
||
13 | const result = await userModel.getBalance(); |
||
14 | setBalance(result); |
||
15 | |||
16 | }; |
||
17 | |||
18 | //Get balance for logged in user |
||
19 | useEffect(() => { |
||
20 | getBalance(); |
||
21 | }); |
||
22 | |||
23 | useEffect(() => { |
||
24 | navigation.addListener('focus', () => getBalance()) |
||
25 | }, []); |
||
26 | |||
27 | async function addBalance() { |
||
28 | setModalVisible(false); |
||
29 | const result = await userModel.addFunds(prepaid); |
||
30 | const updatedBalance = await userModel.getBalance(); |
||
31 | |||
32 | setBalance(updatedBalance); |
||
33 | }; |
||
34 | |||
35 | |||
36 | |||
37 | return ( |
||
38 | <View style={styles.container}> |
||
39 | <View style={styles.titleContainer}> |
||
40 | |||
41 | <Pressable style={[styles.backButton, styles.shadowProp]} onPress={() => navigation.navigate('Map')}> |
||
42 | <Icon |
||
43 | name='x' |
||
44 | size={25} |
||
45 | color='black' |
||
46 | /> |
||
47 | </Pressable> |
||
48 | |||
49 | <Text style={styles.title}>Your balance</Text> |
||
50 | |||
51 | <View style={{width: 50}}></View> |
||
52 | |||
53 | </View> |
||
54 | |||
55 | |||
56 | <View style={[styles.infoContainer]}> |
||
57 | <Text style={styles.balance}>{balance} kr</Text> |
||
58 | <Text style={styles.info}>You should have at least SEK 30 in your wallet to start the journey</Text> |
||
59 | </View> |
||
60 | |||
61 | <View style={styles.prepaidContainer}> |
||
62 | <Text style={styles.prepaidInfo}>Use prepaid card to deposit money</Text> |
||
63 | <Pressable style={[styles.prepaidButton, styles.shadowProp]} onPress={() => setModalVisible(true)}> |
||
64 | <Text style={styles.buttonText} > Use prepaid card </Text> |
||
65 | </Pressable> |
||
66 | </View> |
||
67 | |||
68 | <Modal |
||
69 | animationType="slide" |
||
70 | transparent={true} |
||
71 | visible={modalVisible} |
||
72 | onRequestClose={() => { |
||
73 | setModalVisible(!modalVisible); |
||
74 | }} |
||
75 | > |
||
76 | <View style={styles.modalContainer}></View> |
||
77 | <View style={styles.modalMessage}> |
||
78 | |||
79 | <View style={styles.modalCloseContainer}> |
||
80 | |||
81 | <Pressable style={[styles.closeButton, styles.shadowProp]} onPress={() => setModalVisible(false)}> |
||
82 | <Icon |
||
83 | name='x' |
||
84 | size={15} |
||
85 | color='black' |
||
86 | /> |
||
87 | </Pressable> |
||
88 | |||
89 | </View> |
||
90 | |||
91 | <TextInput |
||
92 | placeholder="Enter prepaid card number" |
||
93 | style={styles.input} |
||
94 | keyboardType="email-address" |
||
95 | onChangeText={(content: string) => { |
||
96 | setPrepaid(content) |
||
97 | }} |
||
98 | |||
99 | onSubmitEditing={() => addBalance() |
||
100 | } |
||
101 | /> |
||
102 | |||
103 | <Pressable style={styles.submitButton} onPress={() => addBalance()}> |
||
104 | <Text style={styles.buttonText}>Submit</Text> |
||
105 | </Pressable> |
||
106 | </View> |
||
107 | </Modal> |
||
108 | |||
109 | |||
110 | </View> |
||
111 | ) |
||
257 | }); |